from rsf.proj import *

Flow('vel','../overthrust_test_2D_2/logs_test_m.rsf',
	'''
	dd form=native | 
     	put label1=Depth  unit1=m label2=Lateral unit2=m
	''')
Plot('vel',
	'''
	grey color=j mean=y title="True model" scalebar=y bartype=v barlabel="V" 
	barunit="m/s"
	''')

Flow('shots','vel',
	'''
	sfgenshots csdgather=n fm=3 amp=1 dt=0.003 ns=30 ng=716 nt=2200
	sxbeg=4 szbeg=4 jsx=23 jsz=0 gxbeg=0 gzbeg=4 jgx=1 jgz=0
	''')
Plot('shots','grey color=g title=shot label2= unit2=',view=1)


Plot('shot4','shots','window n3=1 f3=4| grey color=g title=shot4 label2=Lateral unit2=m')
Plot('shot11','shots','window n3=1 f3=11| grey color=g title=shot11 label2=Lateral unit2=m')
Plot('shot17','shots','window n3=1 f3=17| grey color=g title=shot17 label2=Lateral unit2=m')
Result('shotsnap','shot4 shot11 shot17','SideBySideAniso')

# smoothed velocity model   
#Flow('smvel','vel','smooth repeat=10 rect1=10 rect2=40')

Flow('smvel','../overthrust_test_2D_2/logs_pred.rsf',
	'''
	dd form=native | 
     	put label1=Depth  unit1=m label2=Lateral unit2=m
	''')
Plot('smvel',
     '''
     grey title="Smoothed Marmousi model" wantitle=y allpos=y color=j
     pclip=100 scalebar=y bartype=v barlabel="V" barunit="m/s"
	screenratio=0.45 
     ''' )

Result('marm','vel smvel','TwoRows')

# use the over-smoothed model as initial model for FWI
Flow('vsnaps0 grads0 objs0 illums0','smvel shots',
	'''
	sfgpufwi shots=${SOURCES[1]} grads=${TARGETS[1]} objs=${TARGETS[2]}
	illums=${TARGETS[3]} niter=65 precon=y rbell=10
	''')

Flow('smvel1','vsnaps0',
     '''
     sfwindow f3=63 n3=1
     ''')

Flow('shots1','vel',
	'''
	sfgenshots csdgather=n fm=5 amp=1 dt=0.0025 ns=30 ng=716 nt=2200
	sxbeg=4 szbeg=4 jsx=23 jsz=0 gxbeg=0 gzbeg=4 jgx=1 jgz=0
	''')

Flow('vsnaps grads objs illums','smvel1 shots1',
	'''
	sfgpufwi shots=${SOURCES[1]} grads=${TARGETS[1]} objs=${TARGETS[2]}
	illums=${TARGETS[3]} niter=75 precon=y rbell=10
	''')
Result('vsnaps',
	'''
	grey title="Updated velocity" allpos=y color=j pclip=100 
	scalebar=y bartype=v barlabel="V" barunit="m/s" 
	''')
Plot('mlvel','smvel', 
	'''
	grey color=j mean=y title="multiCMP est." scalebar=y bartype=v barlabel="V" 
	barunit="m/s" 
	''')
Plot('vsnap16','vsnaps', 
	'''
	window n3=1 f3=15|grey title="Updated velocity, iter=16" allpos=y color=j pclip=100 
	scalebar=y bartype=v barlabel="V" barunit="m/s" 
	''')
Flow('fwi1', 'vsnaps0',
     '''
     window n3=1 f3=63
     ''')
Plot('fwi1', 
	'''
	grey color=j mean=y title="FWI" scalebar=y bartype=v barlabel="V" 
	barunit="m/s" 
	''')
Flow('fwi2', 'vsnaps',
     '''
     window n3=1 f3=74
     ''')
Plot('fwi2', 
	'''
	grey color=j mean=y title="precon. FWI" scalebar=y bartype=v barlabel="V" 
	barunit="m/s"
	''')


Result('vsnap','mlvel fwi1 fwi2 vel','TwoRows', view=1)

Result('grads','grey title="Updated gradient" scalebar=y color=j ')
Result('illums','grey title="illumination" scalebar=y color=j')

Result('objs',
	'''
	sfput n2=1 label1=Iteration unit1= unit2= label2= |
	graph title="Misfit function" dash=0 plotfat=5  grid=y yreverse=n
	''')


End()
